package com.open.exam.itemmodel;

import com.open.exam.common.CommonFunction;
import com.open.exam.dto.AnswerModelWithCorr;
import com.open.exam.dto.ItemTemplate;
import com.open.exam.utils.DateUtil;
import com.openexam.openqti.openqtipaperquestion.OpenItemResult;
import com.openexam.openqti.openqtipaperquestion.OpenResultResponseVariable;
import com.openexam.openqti.openqtipaperquestion.OpenScoreOutcomeVariable;
import com.openexam.openqti.qti.BaseTypeType;
import com.openexam.openqti.qti.CardinalityType;
import com.openexam.openqti.qti.SessionStatusType;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 奶茶
 * 完型填空
 * Created by on 2017/12/13.
 */
public class ItemClozeCreater extends IHtmlCreater{

    @Override
    public OpenItemResult save(ItemTemplate template) {
        List<OpenResultResponseVariable> responseList = new ArrayList<>();
        List<OpenScoreOutcomeVariable> outcomeList = new ArrayList<>();

        //考生答案
        List<String> candidateResponses = template.getCandidateResponses();
        //正确答案
        List<String> correctResponses = template.getCorrectResponses();

        Integer index = 1;
        Double scores = 0.0;
        for (ItemTemplate choice:template.getSubItems()) {
            //小题分数
            Double score = CommonFunction.getDoubleDiv(template.getScore(), Double.valueOf(String.valueOf(template.getSubItems().size())));

            OpenResultResponseVariable openResultResponseVariable = new OpenResultResponseVariable(RESPONSE_ + index, CardinalityType.SINGLE, BaseTypeType.IDENTIFIER, correctResponses.get(index - 1), candidateResponses.get(index - 1));
            responseList.add(openResultResponseVariable);

            //小题判分
            //正确得分
            if(candidateResponses.get(index-1).equals(correctResponses.get(index-1))){
                OpenScoreOutcomeVariable openScoreOutcomeVariable = new OpenScoreOutcomeVariable("SCORE_" + index, score, score);
                outcomeList.add(openScoreOutcomeVariable);
                scores += score;
            }else{
                OpenScoreOutcomeVariable openScoreOutcomeVariable = new OpenScoreOutcomeVariable("SCORE_" + index, score, 0);
                outcomeList.add(openScoreOutcomeVariable);
            }

            index++;
        }

        //大题分 判分
        OpenScoreOutcomeVariable openScoreOutcomeVariable = new OpenScoreOutcomeVariable("SCORE", template.getScore(), scores);
        outcomeList.add(openScoreOutcomeVariable);

        return new OpenItemResult(template.getIdentifier(), DateUtil.dateToXmlDate(new Date()), template.getSequenceIndex(), SessionStatusType.INITIAL, 0, template.getMarked(), CommonFunction.getStatus(template.getItemCompletionStatus()), responseList, outcomeList);
    }

    @Override
    public String getPreview(Boolean isShowAnswer) {
        throw new NotImplementedException();
    }

    @Override
    public List<String> getItemResult(OpenItemResult openItemResult) {
        throw new NotImplementedException();
    }

    @Override
    public double judeScore(AnswerModelWithCorr answerModel) {
        throw new NotImplementedException();
    }
}
